package com.mastercard

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;

import grails.converters.JSON

import com.mastercard.Category;
import com.mastercard.auth.Address;
import com.mastercard.auth.User
import com.mastercard.auth.User.Status;
import com.mastercard.common.constants.CommonConstants;
import com.mastercard.common.utils.DateUtil;

class ProfileController {

	def profileService
	def cardService
	
    def index() { }
	
	def getProfile() {
		User loginUsr = SecurityUtils.subject.session.getAttribute(CommonConstants.LOGIN_USER)
		User usr = profileService.getProfileById(loginUsr.id)
		
		Address addr = null
		List addrList = usr.addresses
		if(null != addrList && 0 < addrList.size()){
			for(Address addrTmp: addrList){
				if(Address.Indicator.Y == addrTmp.defaultInd){
					addr = addrTmp
					break
				}
			}
		}

		Map map = new HashMap()
		map.put("id", usr.id)
		map.put("login", usr.login)
		map.put("email", usr.email)
		if(usr.name){
			map.put("name", usr.name)
		}
		if(usr.mobile){
			map.put("mobile", usr.mobile)
		}
		if(usr.homePhone){
			map.put("homePhone", usr.homePhone)
		}
		if(usr.status){
			map.put("status", usr.status.toString())
		}
		if(addr){
			if(addr.addressLine1){
				map.put("addressLine1", addr.addressLine1)
			}
			if(addr.addressLine2){
				map.put("addressLine2", addr.addressLine2)
			}
			if(addr.addressLine3){
				map.put("addressLine3", addr.addressLine3)
			}
			if(addr.addressType){
				map.put("addressType", addr.addressType)
			}
			if(addr.postcode){
				map.put("postcode", addr.postcode)
			}
			if(addr.country){
				map.put("country", addr.country)
			}
		}
		
		def json = new JSON(map)
		render (text:json, contentType:"application/json")
	}
	
	def updateProfile() {
		User loginUser = SecurityUtils.subject.session.getAttribute(CommonConstants.LOGIN_USER)
		profileService.updateUserProfile(loginUser.id, params)
		
		if(params.email){
			loginUser.setEmail(params.email)
		}
		if(params.name){
			loginUser.setName(params.name)
		}
		if(params.mobile){
			loginUser.setMobile(params.mobile)
		}
		
		flash.message = message(code: 'default.updated.message', args: [message(code: 'profile.label', default: 'User Profile'), ''])
	}
	
	def getAllCards() {
		User loginUsr = SecurityUtils.subject.session.getAttribute(CommonConstants.LOGIN_USER)
		User usr = profileService.getProfileById(loginUsr.id);
		List lst = usr.creditCards
		
		def json = new JSON(lst)
		render (text:json, contentType:"application/json")
	}
	
	def getCategories(){
		User loginUsr = SecurityUtils.subject.session.getAttribute(CommonConstants.LOGIN_USER)
		User usr = profileService.getProfileById(loginUsr.id)
		List lst = usr.categories
				
		def json = new JSON(lst)
		render (text:json, contentType:"application/json")
	}
	
	def getDeals(){
		User loginUsr = SecurityUtils.subject.session.getAttribute(CommonConstants.LOGIN_USER)
		User usr = profileService.getProfileById(loginUsr.id)
		List catList = usr.categories
		
		List lst = new ArrayList()
		for(int i=0;i<catList.size();i++){
			Category cat = catList.get(i)
			lst.add(cat.deals)
		}
				
		def json = new JSON(lst)
		render (text:json, contentType:"application/json")
	}
	
	def createCard() {
		User loginUsr = SecurityUtils.subject.session.getAttribute(CommonConstants.LOGIN_USER)
		User usr = profileService.getProfileById(loginUsr.id)
		cardService.saveCard(params, usr)
		
		flash.message = message(code: 'default.created.message', args: [message(code: 'creditCard.label', default: 'Credit Card'), ''])
	}
	
	def updateCard() {
		cardService.saveCard(params, null)
		
		flash.message = message(code: 'default.updated.message', args: [message(code: 'creditCard.label', default: 'Credit Card'), ''])
	}
	
	def deleteCard() {
		cardService.deleteCardById(params.id)
		
		flash.message = message(code: 'default.deleted.message', args: [message(code: 'creditCard.label', default: 'Credit Card'), ''])
	}
}
